package cn.dao789.mvc;

import cn.dao789.model.UserModel;

/**
 * MVC架构中的Controller
 * 负责处理用户输入、更新Model，并通知View更新
 */
public class UserController {
    private UserModel userModel;
    private MvcActivity mvcActivity;
    
    public UserController(UserModel userModel, MvcActivity mvcActivity) {
        this.userModel = userModel;
        this.mvcActivity = mvcActivity;
    }
    
    // 处理保存用户的业务逻辑
    public void saveUser(String username, String email) {
        // 验证输入
        if (username == null || username.isEmpty()) {
            username = "未命名用户";
        }
        
        if (email == null || email.isEmpty()) {
            email = "noemail@example.com";
        }
        
        // 更新Model
        userModel.setUsername(username);
        userModel.setEmail(email);
        
        // 通知View更新UI
        mvcActivity.updateUserInfo();
    }
    
    // 获取用户信息的方法
    public UserModel getUser() {
        return userModel;
    }
}